/*
 *  Copyright 2013 HCIWriter
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.hci.writer.client.activity;

import java.util.ArrayList;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.hci.writer.client.ClientFactory;
import com.hci.writer.client.Storage;
import com.hci.writer.client.place.AdminPlace;
import com.hci.writer.client.place.GreetingPlace;
import com.hci.writer.client.place.KeyboardHwPlace;
import com.hci.writer.client.place.KeyboardSwPlace;
import com.hci.writer.client.place.SavePlace;
import com.hci.writer.client.place.WritePlace;
import com.hci.writer.client.ui.GreetingView;
import com.hci.writer.shared.Participant;
import com.hci.writer.shared.Task;
import com.hci.writer.shared.Taskset;
import com.hci.writer.shared.tasks.TasksetA;
import com.hci.writer.shared.tasks.TasksetB;
import com.hci.writer.shared.tasks.TrainingTaskHw;
import com.hci.writer.shared.tasks.TrainingTaskSw;

public class GreetingActivity extends AbstractActivity implements GreetingView.Presenter {

	private ClientFactory clientFactory;
	private GreetingView greetingView;
	
	public GreetingActivity(GreetingPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }
	
	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		this.greetingView = clientFactory.getGreetingView();
		this.greetingView.setPresenter(this);
        containerWidget.setWidget(this.greetingView.asWidget());
	}
	
	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

	@Override
	public void startMeasurement() {
		String key = this.greetingView.getStartKey();
		
		if(key.length() == 0) {
			this.greetingView.wrongStartKey("Der Startschlüssel darf nicht leer sein.");
		} else 	if(key.equals("admin")) {
			goTo(new AdminPlace(""));
		} else if(!key.matches("^[0-9]+$")) {
			this.greetingView.wrongStartKey("Der Startschlüssel muss ganzzahlig sein.");
		} else {
			Storage.participant = new Participant(key);
			int keyMod = Integer.parseInt(key)%4;
			
			if(keyMod == 0) {
				Storage.placeStack.offer(new KeyboardHwPlace(""));
				Storage.placeStack.offer(new WritePlace(new TrainingTaskHw()));
				addTaskset(new TasksetA());
				
				Storage.placeStack.offer(new KeyboardSwPlace(""));
				Storage.placeStack.offer(new WritePlace(new TrainingTaskSw()));
				addTaskset(new TasksetB());
				
				Storage.placeStack.offer(new SavePlace(""));
				goTo(Storage.placeStack.poll());
			} else if(keyMod == 1) {
				Storage.placeStack.offer(new KeyboardHwPlace(""));
				Storage.placeStack.offer(new WritePlace(new TrainingTaskHw()));
				addTaskset(new TasksetB());
				
				Storage.placeStack.offer(new KeyboardSwPlace(""));
				Storage.placeStack.offer(new WritePlace(new TrainingTaskSw()));
				addTaskset(new TasksetA());
				
				Storage.placeStack.offer(new SavePlace(""));
				goTo(Storage.placeStack.poll());
			} else if(keyMod == 2) {
				Storage.placeStack.offer(new KeyboardSwPlace(""));
				Storage.placeStack.offer(new WritePlace(new TrainingTaskSw()));
				addTaskset(new TasksetA());
				
				Storage.placeStack.offer(new KeyboardHwPlace(""));
				Storage.placeStack.offer(new WritePlace(new TrainingTaskHw()));
				addTaskset(new TasksetB());
				
				Storage.placeStack.offer(new SavePlace(""));
				goTo(Storage.placeStack.poll());
			} else if(keyMod == 3) {
				Storage.placeStack.offer(new KeyboardSwPlace(""));
				Storage.placeStack.offer(new WritePlace(new TrainingTaskSw()));
				addTaskset(new TasksetB());
				
				Storage.placeStack.offer(new KeyboardHwPlace(""));
				Storage.placeStack.offer(new WritePlace(new TrainingTaskHw()));
				addTaskset(new TasksetA());
				
				Storage.placeStack.offer(new SavePlace(""));
				goTo(Storage.placeStack.poll());
			}
		}
	}

	private void addTaskset(Taskset ts) {
		ArrayList<Task> taskList = ts.getTasks();
		for(Task task : taskList) {
			Storage.participant.addTask(task);
			Storage.placeStack.offer(new WritePlace(task));
		}
	}
}
